package com.liangtian.mdm.material.controller;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.liangtian.mdm.base.controller.BaseController;
import com.liangtian.mdm.base.utils.ResultObject;
import com.liangtian.mdm.base.utils.ShiroSessionUtils;
import com.liangtian.mdm.dm.common.pojo.ResponseMsg;
import com.liangtian.mdm.dm.common.util.RequestUtils;
import com.liangtian.mdm.material.model.WlwlField;
import com.liangtian.mdm.material.model.WlwlFieldSelectValue;
import com.liangtian.mdm.material.service.WlwlFieldSelectValueService;
import com.liangtian.mdm.material.service.WlwlFieldService;
import com.liangtian.mdm.system.log.SysLogUtil;
import com.liangtian.mdm.system.log.model.SysLogEnum;
import com.liangtian.mdm.system.user.model.SysUser;
import com.liangtian.mdm.util.ConstantBean;

@Controller
@RequestMapping("/wlwlField")
public class WlwlFieldController extends BaseController {

    @Autowired
    private WlwlFieldService wlwlFieldService;

    @Autowired
    private WlwlFieldSelectValueService selectValueService;

    // Session中保存的当前登录用户的信息
    //@Value("${ConstantBean.CURRENT_SESSION_USER}")
    //private String ConstantBean.CURRENT_SESSION_USER;

    @SuppressWarnings("rawtypes")
    @RequestMapping(value = "/queryWlwlField", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject queryWlwlField(HttpServletRequest request) {
        Map<String, Object> paramMap = RequestUtils.paramToMap(request);

        PageInfo pageInfo = wlwlFieldService.queryWlwlField(paramMap);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("rows", pageInfo.getList());
        jsonObject.put("total", pageInfo.getTotal());
        return jsonObject;
    }

    @RequestMapping(value = "/toEditWlwlField", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject toEditWlwlField(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String field = request.getParameter("field");
        WlwlField wlwlField = wlwlFieldService.queryWlwlFieldById(field);
        jsonObject.put("wlwlField", wlwlField);
        return jsonObject;
    }

    @RequestMapping(value = "/updateWlwlField", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject updateWlwlField(HttpServletRequest request) {
        Map<String, Object> paramMap = RequestUtils.paramToMap(request);
        // 获取session中用户
        SysUser user = ShiroSessionUtils.getSessionSysUser(ConstantBean.CURRENT_SESSION_USER);
        paramMap.put("updateAccount", user.getUserAccount());
        paramMap.put("updateName", user.getUserName());
        paramMap.put("updateTime", new Date().toString());
        JSONObject jsonObject = new JSONObject();
        boolean suc = false;
        int count = wlwlFieldService.updateWlwlField(paramMap);
        if (1 == count)
            suc = true;
        jsonObject.put("success", suc);
        SysLogUtil.insertLog("编辑物料配置字段",
                SysLogEnum.WL_WL_FEILD.getCode(), SysLogEnum.BJ.getCode(), request);
        return jsonObject;
    }

    //--------------------------物料配置字段下拉选项值------------------------//

    @SuppressWarnings("rawtypes")
    @RequestMapping(value = "/getListByCond", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject getListByCond(HttpServletRequest request) {
        Map<String, Object> paramMap = RequestUtils.paramToMap(request);

        PageInfo pageInfo = selectValueService.getListByCond(paramMap);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("rows", pageInfo.getList());
        jsonObject.put("total", pageInfo.getTotal());
        return jsonObject;
    }

    @RequestMapping(value = "/insertWlwlFieldSelectvalue", method = RequestMethod.POST)
    @ResponseBody
    public ResultObject insertWlwlFieldSelectvalue(WlwlFieldSelectValue value, HttpServletRequest request) {
        WlwlFieldSelectValue val = new WlwlFieldSelectValue();
        val.setField(value.getField());
        int num = selectValueService.getCountByMap(val);
        int xh = 0;
        if (0 == num) {
            xh = num + 1;
        } else {
            int mx = selectValueService.getCountByMap(val);
            xh = mx + 1;
        }
        value.setOrderNum(String.valueOf(xh));
        // 获取session中用户
        SysUser user = ShiroSessionUtils.getSessionSysUser(ConstantBean.CURRENT_SESSION_USER);
        value.setCreateUid(String.valueOf(user.getUserId()));
        value.setCreateUname(user.getUserName());
        value.setUpdateUid(String.valueOf(user.getUserId()));
        value.setUpdateUname(user.getUserName());
        selectValueService.insertWlwlFieldSelectvalue(value);
        SysLogUtil.insertLog("新增物料配置字段[" + value.getField() + "]下拉选项值[" + value.getFieldValue() + "]下拉选项文本[" + value.getFieldText() + "]",
                SysLogEnum.WL_WL_FIELD_SELECTVALUE.getCode(), SysLogEnum.XZ.getCode(), request);
        ResultObject resultObject = new ResultObject(1, "新增成功", "新增失败");
        return resultObject;
    }

    @ResponseBody
    @RequestMapping(value = "/getCountByMap", method = RequestMethod.POST)
    public JSONObject getCountByMap(HttpServletRequest request, WlwlFieldSelectValue value) {
        JSONObject jsonObject = new JSONObject();
        ResponseMsg<String> rMsg = new ResponseMsg<String>();
        try {
            int count = selectValueService.getCountByMap(value);
            rMsg.setRspCode(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        jsonObject.put("rmsg", rMsg);
        return jsonObject;
    }

    @ResponseBody
    @RequestMapping(value = "/getCountByMapToEdit", method = RequestMethod.POST)
    public JSONObject getCountByMapToEdit(HttpServletRequest request, WlwlFieldSelectValue value) {
        JSONObject jsonObject = new JSONObject();
        ResponseMsg<String> rMsg = new ResponseMsg<String>();
        try {
            int count = selectValueService.getCountByMapToEdit(value);
            rMsg.setRspCode(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        jsonObject.put("rmsg", rMsg);
        return jsonObject;
    }

    @ResponseBody
    @RequestMapping(value = "/getValueById", method = RequestMethod.POST)
    public String getValueById(WlwlFieldSelectValue value) {
        WlwlFieldSelectValue val = null;
        try {
            val = selectValueService.getValueById(value);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSON.toJSONString(val);
    }

    @ResponseBody
    @RequestMapping(value = "/updateWlFieldSelVal", method = RequestMethod.POST)
    public JSONObject updateWlFieldSelVal(WlwlFieldSelectValue value, HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        ResponseMsg<String> rMsg = new ResponseMsg<String>();
        try {
            // 获取session中用户
            SysUser user = ShiroSessionUtils.getSessionSysUser(ConstantBean.CURRENT_SESSION_USER);
            WlwlFieldSelectValue queryVal = new WlwlFieldSelectValue();
            queryVal.setId(value.getId());
            WlwlFieldSelectValue val = selectValueService.getValueById(queryVal);
            String oldval = val.getFieldValue();
            String oldText = val.getFieldText();
            val.setFieldValue(value.getFieldValue());
            val.setFieldText(value.getFieldText());
            val.setCond(value.getCond());
            val.setRemark(value.getRemark());
            val.setUpdateUid(String.valueOf(user.getUserId()));
            val.setUpdateUname(user.getUserName());
            selectValueService.updateWlFieldSelVal(val);
            rMsg.setRspCode(0);
            //记录日志
            String logContent = "编辑物料配置字段[" + value.getField() + "]";
            if (!oldval.equals(value.getFieldValue())) {
                logContent += "下拉选项值从[" + oldval + "]改为[" + value.getFieldValue() + "]";
            }
            if (!oldText.equals(value.getFieldText())) {
                logContent += "下拉选项文本从[" + oldText + "]改为[" + value.getFieldText() + "]";
            }
            SysLogUtil.insertLog(logContent, SysLogEnum.WL_WL_FIELD_SELECTVALUE.getCode(),
                    SysLogEnum.BJ.getCode(), request);
        } catch (Exception e) {
            e.printStackTrace();
            rMsg.setRspCode(-1);
        }
        jsonObject.put("rmsg", rMsg);
        return jsonObject;
    }

}
